import {
  ds
} from "../../../../../tools/data"
import {
  users
} from "../../../../../tools/users"
import {
  pages
} from '../../../../../tools/pages'
import {
  nav2
} from "~/tools/nav2";

Page({
  data: {
    action: 'new',
    index: 0,
    id: '',
    item: {
      showTicket: false
    },
    mod: {},
    errMsg: ""
  },
  async init() {
    const {
      id,
      index
    } = this.data
    const that = this
    ds.coll('module').where({
      _id: ds.cmd.eq(id)
    }).get({
      success: function (res) {
        if (res.data && res.data.length > 0) {
          that.setData({
            mod: res.data[0],
            item: res.data[0].slides[index]
          })
        }
      }
    });

  },
  selImg() {
    wx.navigateTo({
      url: '../../../../common/imgSelect/index'
    })
  },
  addImgClick() {
    this.selImg()
  },
  imgClick() {
    this.selImg()
  },
  imgSelected(files) {
    if (files.length > 0) {
      this.setData({
        'item.img': files[0]
      })
    }
  },
  actionTaped() {
    const {
      item
    } = this.data
    nav2.navigate(`/pages/usercenter/module/action/index?data=${JSON.stringify(item)}`)
  },
  setAction(data) {
    this.setData({
      item: data
    })
  },
  saveClick() {
    const {
      action,
      id,
      index,
      item,
      mod
    } = this.data
    if (action == 'edit') {
      ds.cloudUpdate('module', id, {
        [`slides.${index}`]: this.data.item
      }, res => {
        pages.back(prePage => {
          prePage.init()
        })
      })
    } else {
      mod.slides = mod.slides ?? []
      mod.slides.push(item)
      ds.cloudUpdate('module', id, mod, res => {
        pages.back(prePage => {
          prePage.init()
        })
      })
    }
  },
  delClick() {
    const {
      id,
      mod,
      index
    } = this.data
    mod.slides.splice(index)
    ds.cloudUpdate('module', id, mod, res =>
      pages.back(prePage => {
        prePage.init()
      }))
  },
  onChooseLink() {
    wx.navigateTo({
      url: '/pages/common/goodsManage/index',
    })
  },
  onLinkTap(e) {
    const link = e.target.dataset.link
    if (!link) return
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${link}`,
    })
  },
  showTicketTap() {
    const item = this.data.item
    item.showTicket = !item.showTicket
    this.setData({
      item
    })
  },
  visibleTap() {
    const item = this.data.item
    this.setData({
      'item.visible': !item.visible
    })
  },
  nav2PromotionTap() {
    const item = this.data.item
    item.showPromotion = !item.showPromotion
    if (item.showPromotion) item.link = ""
    this.setData({
      item
    })
  },
  onTextChange(e) {
    const text = e.detail.value
    const {
      item
    } = this.data
    item.text = text
    this.setData({
      item
    })
  },
  onLoad(option) {
    const data = this.data
    this.setData({
      action: option.action ? option.action : data.action,
      index: option.index,
      id: option.id
    })

    this.init();
  },
});